package com.github.chain.admin.api.controller;

import com.github.chain.admin.api.util.HttpServletUtil;
import com.github.chain.admin.client.MenuServiceClient;
import com.github.chain.admin.common.domain.Menu;
import com.github.chain.admin.common.domain.User;
import com.github.chain.admin.common.group.InserterGroup;
import com.github.chain.admin.common.group.UpdateGroup;
import com.github.chain.admin.common.request.MenuRequest;
import com.github.framework.core.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author AIQ
 * @date 2023/5/18
 * @project er-trader
 */
@Controller
public class MenuController {

    @Resource
    private MenuServiceClient menuServiceClient;

    @GetMapping("/main/system/menu/index")
    @RequiresPermissions("system:menu:index")
    public String index(Model model){

        String search = HttpServletUtil.getRequest().getQueryString();

        model.addAttribute("title", search);
        return "/manager/menu/index";
    }

    @GetMapping("/system/menu/list")
    @RequiresPermissions("system:menu:list")
    @ResponseBody
    public Result<List<Menu>> menuList(MenuRequest menuRequest){

        return menuServiceClient.findMenuList(menuRequest);
    }

    @GetMapping(value = {"/system/menu/add", "/system/menu/add/{id}"})
    @RequiresPermissions("system:menu:add")
    public String add(@PathVariable(value = "id", required = false) Long id, Model model){

        if (id != null){
            Result<Menu> result = menuServiceClient.findMenuById(id);

            if (result.isSuccess()){
                model.addAttribute("pMenu", result.getData());
            }
        }
        return "/manager/menu/add";
    }

    @GetMapping("/system/menu/sortList/{pid}/{id}")
    @RequiresPermissions("system:menu:add")
    @ResponseBody
    public Result<Map<Integer, String>> sortList(@PathVariable(value = "pid", required = false) Long pid, @PathVariable(value = "id", required = false) Long id){

        MenuRequest menuRequest = new MenuRequest();
        menuRequest.setId(id);
        menuRequest.setPid(pid);

        return menuServiceClient.findMenuListSort(menuRequest);
    }

    @PostMapping("/system/menu/save")
    @RequiresPermissions("system:menu:save")
    @ResponseBody
    public Result saveMenu(@Validated(value = InserterGroup.class) MenuRequest menuRequest){

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        menuRequest.setCreateBy(user.getId());
        return menuServiceClient.saveMenu(menuRequest);
    }

    @GetMapping("/system/menu/edit/{id}")
    @RequiresPermissions("system:menu:edit")
    public String showEditMenu(@PathVariable("id") Long id, Model model){
        Result<Menu> menuById = menuServiceClient.findMenuByMenuId(id);
        model.addAttribute("menu", menuById.getData());
        return "/manager/menu/edit";
    }

    @PostMapping("/system/menu/edit")
    @RequiresPermissions("system:menu:edit")
    @ResponseBody
    public Result<Integer> editMenu(@Validated(value = UpdateGroup.class) MenuRequest menuRequest){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        menuRequest.setUpdateBy(user.getId());
        return menuServiceClient.updateMenu(menuRequest);
    }

    @GetMapping("/system/menu/detail/{id}")
    @RequiresPermissions("system:menu:detail")
    public String detailMenu(@PathVariable("id") Long id, Model model){
        Result<Menu> menuById = menuServiceClient.findMenuByMenuId(id);
        model.addAttribute("menu", menuById.getData());
        return "/manager/menu/detail";
    }

    @GetMapping("/system/menu/delete/{id}")
    @RequiresPermissions("system:menu:detail")
    @ResponseBody
    public Result<Integer> deleteMenu(@PathVariable("id") Long id){
        return menuServiceClient.deleteMenuByMenuId(id);
    }
}
